﻿using System;
using System.Collections.Generic;
using Common.Application.Transactions;
using Products.Domain.Products;
using Products.Dto.Product;

namespace Products.Application.Products
{
    public class ProductCommandService : IProductCommandService
    {

        private readonly IProductRepository _productRepository;
        private readonly IProduceFactory _produceFactory;

        public ProductCommandService(IProductRepository productRepository, IProduceFactory produceFactory)
        {
            _productRepository = productRepository;
            _produceFactory = produceFactory;
        }


        [Transaction]
        public Guid CreateNewProduce(CreateNewProductParametersDto parameters)
        {
            var newProduce = _produceFactory.CreateNewProduce(parameters);
            _productRepository.Add(newProduce);
            return newProduce.Id;
        }

        [Transaction]
        public void ChangeProductPriceForRandom(Guid produceId)
        {
            var produce = _productRepository.Get(produceId);
            produce.ChangePrice(new Random().Next());
            _productRepository.Update(produce);
        }

        public IEnumerable<Produce> GetAllProduce()
        {
            return _productRepository.GetAllProduces();
        }

        
    }

}
